
import { loadLanguageAsync } from "@/locale/index";
import { toRaw } from "vue";
import shareds from "@/common/utils/shared";

const persistedData = {
  state: {
    lang: "", //当前语言
    collapsed: false, // 左侧菜单是否折叠
    appShareData: {}, // 主子应用共享数据

    currentTopMenu: {}, // 当前选中的顶级菜单
    currentSubMenuName: "", // 当前选中的子集菜单
    currentApp: {}, // 当前选中的应用菜单

    incomeBillsBeforeDate: false, //账单列表：收款账单，显示/隐藏未到应收期账单
    outcomeBillsBeforeDate: false, //账单列表：付款账单，显示/隐藏未到应付期账单
    matchBillsShowOtherOwner: false,//显示/隐藏其他租客账单
    matchTurnoverShowOtherOwner: false, //显示/隐藏其他租客收支流水
  },
  mutations: {
    APP_LANG: (state, lang, antd = {}) => {
      state.lang = lang;
      state._antLocale = antd;
    },
    SET_CURRENT_TOP_MENU: (state, topMenu) => {
      state.currentTopMenu = topMenu;
    },
    SET_CURRENT_SUB_MENU_NAME: (state, menuName) => {
      state.currentSubMenuName = menuName;
    },
    SET_CURRENT_APP: (state, app) => {
      state.currentApp = app;
    },

    INIT_CURRENT_MENU: (state, menu = {}) => {
      state.currentMenu = menu;
      state.currentSubMenuName = "";
    },
    SET_SAHRE_DATA: (state, data) => {
      state.appShareData = data;
    },
    SET_COLLAPSED: (state, collapsed) => {
      state.collapsed = collapsed;
    },

    SET_INCOME_BILLS_BEFORE_DATE: (state, data) => {
      state.incomeBillsBeforeDate = data;
    },
    SET_OUTCOME_BILLS_BEFORE_DATE: (state, data) => {
      state.outcomeBillsBeforeDate = data;
    },
    SET_MATCH_BILLS_SHOW_OTHER_OWNER: (state, data) => {
      state.matchBillsShowOtherOwner = data;
    },
    SET_MATCH_TURNOVER_SHOW_OTHER_OWNER: (state, data) => {
      state.matchTurnoverShowOtherOwner = data;
    },
  },
  actions: {
    //   设置多语言
    SetLang({ commit }, lang) {
      return new Promise((resolve, reject) => {
        loadLanguageAsync(lang)
          .then(() => {
            commit("APP_LANG", lang);
            resolve();
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    /**
     * @description: 设置选中菜单
     */
    // 当前选中的顶级菜单
    SetCurrentTopMenu: ({ commit }, menu) => {
      return new Promise((resolve, reject) => {
        commit("SET_CURRENT_TOP_MENU", menu);
        commit("SET_CURRENT_APP", {});
        shareds.consoleInfo(toRaw(menu), "顶级菜单选中");
        resolve();
      });
    },
    // 当前选中的子菜单名称
    SetCurrentSubMenuName: ({ commit }, menu) => {
      return new Promise((resolve, reject) => {
        commit("SET_CURRENT_SUB_MENU_NAME", menu.alias);
        shareds.consoleInfo(toRaw(menu), "子级菜单选中");
        resolve();
      });
    },
    SetCurrentApp({ commit }, app) {
      return new Promise((resolve, reject) => {
        commit("SET_CURRENT_APP", app);
        commit("SET_CURRENT_TOP_MENU", {});
        shareds.consoleInfo(app.app_name, "菜单内应用选中");
        resolve();
      });
    },
    ClearCurrentMenu({ commit }) {
      commit("SET_CURRENT_TOP_MENU", {});
      commit("SET_CURRENT_SUB_MENU_NAME", "");
    },
    initCurrentMenuInfo({ commit, state, getters }, { name }) {
      let { currentMenu } = state;
      if (name && currentMenu.alias != name) {
        currentMenu = getters.menuList.find((item) => item.alias == name);
      }
      commit("INIT_CURRENT_MENU", currentMenu);
    },
    /**
     * @description: 存储应用共享数据
     */
    SetAppShareData({ commit }, data) {
      commit("SET_SAHRE_DATA", data);
    },
    /**
     * @description: 是否折叠子菜单
     * @param {*} collapsed
     */
    SetCollapsed({ commit }, collapsed) {
      commit("SET_COLLAPSED", collapsed);
    },

    /**
     * @description: 是否折叠子菜单
     */
    SetIncomeBillsBeforeDate: ({ commit }, data) => {
      commit("SET_INCOME_BILLS_BEFORE_DATE", data);
    },
    SetOutcomeBillsBeforeDate: ({ commit }, data) => {
      commit("SET_OUTCOME_BILLS_BEFORE_DATE", data);
    },
    SetMatchBillsShowOtherOwner: ({ commit }, data) => {
      commit("SET_MATCH_BILLS_SHOW_OTHER_OWNER", data);
    },
    setMatchTurnoverShowOtherOwner: ({ commit }, data) => {
      commit("SET_MATCH_TURNOVER_SHOW_OTHER_OWNER", data);
    },
  },
};

export default persistedData;
